#Copyright (c) 2025, Alibaba Cloud and its affiliates;
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at

#   http://www.apache.org/licenses/LICENSE-2.0

#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.



import json

import fce_util.deploys.resource_mgr as resource
import fce_util.libs.constants as constants
from fce_util.libs import log as loggers
from fce_util.fceres import fce_res_common

logger = loggers.g_logger.logger

def resources_clean(args):
    """
    FCE Ray cluster dependency resources clean
    """
    logger.debug('resources_clean enter,args:{}'.format(args))
    if not args.cluster_id:
        stdout = "Cluster id must not be null"
        logger.error(stdout)
        fce_res_common(success = False, errcode = 'InvalidParameters.Error', errmsg = stdout)
        return

    if not args.cluster_name:
        stdout = "Cluster name must not be null"
        logger.error(stdout)
        fce_res_common(success = False, errcode = 'InvalidParameters.Error', errmsg = stdout)
        return

    namespace = args.namespace if args.namespace else constants.DEFAULT_NAMESPACE
    logger.info('clean configMap start')
    
    cluster_id = args.cluster_id
    if resource.checkServiceExist('rc', args.cluster_name, namespace):
        stdout = f"Deployment: {args.cluster_name} Exist..."
        logger.error(stdout)
        fce_res_common(success = False, errcode = 'RayCluster.CleanResourcesFailed', errmsg = stdout)
        return

    if resource.checkServiceExist('cm', resource._getName(cluster_id, 'cm'), namespace):
        resource.clean('cm', resource._getName(cluster_id, 'cm'), namespace)
        logger.info(f"configMap: {resource._getName(cluster_id, 'cm')} clean successful")
    else:
        stdout = f"configMap: {resource._getName(cluster_id, 'cm')} not found..."
        logger.warn(stdout)
        #fce_res_common(success = False, errcode = 'RayCluster.CleanResourcesFailed', errmsg = stdout)
        #return

    msg = {'Success': True}
    msg['Message'] = {
        'Msg': 'Ray cluster dependency resources clean success'
    }
    print(json.dumps(msg))

def FCE_resources_clean(args):
    if args.clean == 'res_obj':
        resources_clean(args)
        return
    args.this_parser.print_help()

def make(parser):
    """
    FCE Ray Cluster List entry
    """
    parser.add_argument( 
        '--cluster_name',
        dest='cluster_name',
        nargs = '?',
        metavar = ('CLUSTER_NAME'),
        help='Ray Cluster Name',
        required=True,
        )
    
    parser.add_argument( 
        '--cluster_id',
        dest='cluster_id',
        nargs = '?',
        metavar = ('CLUSTER_ID'),
        help='Ray Cluster Id',
        required=True,
        )
    
    parser.add_argument( 
        '--namespace',
        dest='namespace',
        nargs = '?',
        metavar = ('NAMESPACE'),
        help='Ray Cluster Namespace',
        required=False,
        )
    
    parser.add_argument( 
        '--conf_path',
        dest='conf_path',
        nargs = '?',
        metavar = ('CONF_PATH'),
        help='Kube Config Path',
        required=False,
        )
    
    parser.set_defaults(
        func=FCE_resources_clean,
        this_parser=parser
    )
